<?php
/*
维修
*/
class Con_service extends Acan{
	public $defaultAct='list';
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		if(empty($this->id) || $this->id == '*'){$this->id = '0';}
		$this->model = Afun_db()->model(Adb_asale_service);
	}
	function Act_search(){
		$key = $_POST['key'];
		$types = $_POST['type'];
		if(!empty($key)){
			if(is_numeric($key)){
				if(preg_match("/^1(3|4|5|6|7|8|9)[0-9]{9}$/",$key)){
					$type = 'phone';
					$wtype = "`type` = 'phone'";
				}elseif(strlen($key)==15){
					$type = 'imei';
					$wtype = "`type` = 'imei'";
				}else{
					$fuzzy = 1;
					$wtype = "(`type` = 'phone' or `type` = 'imei')";
				}
			}else{
				$type = 'name';
				$wtype = "`type` = 'name'";
			}
			//--模糊查找
			if($fuzzy==1){
				$wdata = " and `data` LIKE '%".$key."%'";
			}else{
				$wdata = " and `data` = '".$key."'";
			}
			$con->where = $wtype.$wdata;
			$con->join = "LEFT JOIN `".Acan_table_asale_user."` u ON a.`uid`=u.`uid`";
			$this->userinfo = Afun_db()->model(Acan_table_asale_userinfo);
			$this->user = Afun_db()->model(Acan_table_asale_user);
			$data = $this->userinfo->findAll($con);
			if(empty($data)){
				if($fuzzy==1){
					$error = 1;
					$msg = '找不到!';
				}else{
				//	$msg = '是否新建用户?';
					$msg = $this->user_add($type,$key);
					return;
				}
			}else{
				$msg = '用户列表';
			}
		}else{
			$error = 1;
			$msg = '请填写内容!';
		}
		$json->error = $error;
		$json->msg = $msg;
		Afun_json($json);
	}
	function user_add($type,$key){
		$search_type = Afunc_enum()->data('asale_search_type');
		$title = $search_type[$type];
		include $this->Ahtml('order/user_add');
	}
	function Act_save_add($type,$key){
		$search_type = Afunc_enum()->data('asale_search_type');
		$title = $search_type[$type];
		include $this->Ahtml('order/user_add');
	}
	//--下单
	function Act_order(){
		$search_type = Afunc_enum()->data('asale_search_type');
		include $this->Atpl();
	}
	//--列表
	function Act_list(){
		$obj = Afun_acan_new('list');
		$key_name = $this->model->key_name;
		$columnrr = $this->model->comment;//初始栏目
		$column = $obj->column($this->model,$columnrr,$_POST['acolumn']);
		$column_html = $obj->column_html($this->model,$column);
		$column_json = $obj->form_json($this->model);
		if(empty($this->title)){$this->title = '订单列表';}
		$form_val_status = array('1'=>'开始交易','2'=>'交易中','3'=>'成功','4'=>'失败');
		//分页函数
	//	$con->where = "`pid`='$this->id'";
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = $this->model->key_name;
		$page->page = '20';
		$page->counts = $this->model->count($con);	//统计总数目
		$pages = Afun_apage($page);
		$con->order = "a.$pages->orderby";
		$con->limit = $pages->limit;
		//分页结束
		$list = $this->model->findAll($con);
		include $this->Atpl();
	}
	//批量更新
	function Act_save(){
		Afun_post_auth();
		if($this->id == 'one'){
			$save = $_POST['save'];
			$key = $_POST['save'][$this->model->key_name];
			unset($_POST['save'][$this->model->key_name]);
			if(!empty($key)){
				$msg = $this->model->updatePk($save,$key);
			}else{
				$msg = $this->model->save($save);
			}
		}elseif(is_array($_POST['save'])){
			foreach($_POST['save'] as $i=>$vals){
				$this->model->updatePk($vals,$vals['key']);
			}
		}
		echo $msg;
	}
	//添加
	function Act_add(){
		Afun_post_auth();
		$_POST['add']['pid'] = $this->id;
		$_POST['add']['class'] = $_POST['add']['class']+1;
		if($this->model->save($_POST['add'])){
			Afun_refresh('添加成功','referrer');
		}else{Afun_refresh('添加失败','referrer');}
			
	}
	//删除
	function Act_del(){
		Afun_post_auth();
		if($this->model->deletePk($this->id)){
			Afun_refresh('删除成功','referrer');
		}
	}
	//删除所选
	function Act_delall(){
		Afun_post_auth();
		foreach($_POST['chk'] as $key=>$val){
			if(empty($vala))
				$vala = "'$val'";
			else
				$vala .=",'$val'";
		}
		$this->model->delete($this->model->key_name." IN ($vala)");
		Afun_refresh("删除成功",'referrer');
	}
	
}

?>